<?php
class IndexController extends Ctrl_Base {

	public function indexAction() {
		$this->display("index");
	}

	public function mtestAction(){
		$this->display("mtest");
	}

	public function logregAction(){
		$this->display("logreg");
	}

	public function check_wxAction(){
		//get post data, May be due to the different environments
		$postStr = GLOBALS["HTTP_RAW_POST_DATA"];
		Funs_Base::cdlog($postStr);
      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername =strval($postObj->FromUserName);
                $toUsername = strval($postObj->ToUserName);
                $keyword = trim(strval(($postObj->Content)));
                $msgType = strval($postObj->MsgType);
                $time = time();
               
                if($keyword){
                	switch ($msgType) {
	                	case 'text':
	                		$wxcfg=Yaf_Registry::get("config")->wx->text->toArray();
	                		
	                		if(in_array($keyword, explode(",", $wxcfg["menu"]))){
	                			$sns=new Sns_Wx($keyword);
	                			$sns->send_msg($toUsername,$fromUsername);
	                		}

	                		break;
	                	
	                	case 'unsubscribe':
	                		# code...
	                		break;
	                	case 'subscribe':
	                }
                }
               else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
	}

	public function catchAction(){
		$rs=Funs_Base::http("http://www.qiushibaike.com/hot");
		#echo ($rs);
		$list_pat="/<div.+?id=[\'\"]qiushi_tag.+?>(.+?)<div\s+id=[\"\']qiushi_counts/is";
		$content_pat="/<div\s+class=[\'\"]content.+?>(.+?)<\/div/is";
		$thumb_pat="/<div\s+class=[\'\"]thumb.+?img\s+src=[\'\"](.+?)[\'\"]/is";
		preg_match_all($list_pat, $rs,$match);

		$i=0;
		$items=array();
		foreach($match[1] as $v){
			if($i==10) break;
			preg_match($content_pat,$v,$title_arr);
			preg_match($thumb_pat, $v,$thumb_arr);
			$items[$i]["content"]=$title_arr[1];
			if(!empty($thumb_arr))
				$items[$i]["thumb"]=$thumb_arr[1];
			else $items[$i]["thumb"]="";
			$i++;
		}

		print_r($items);
	}
}
